package com.ztgf.sys.api.client;


import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.sys.models.dto.SysAreaDTO;
import com.ztgf.sys.models.service.ISysAreaService;
import com.ztgf.sys.models.vo.SysAreaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 区域  前端控制器
 * </p>
 *
 * @author YUE
 * @since 2020-05-25
 */
@RestController
@RequestMapping("/api/sysArea")
@Slf4j
@Api(value = "SysAreaApiController",tags = "区域表接口")
public class SysAreaApiController {

    @DubboReference
    private ISysAreaService iSysAreaService;


    @PostMapping("/selectAreaNameBatch")
    @ApiOperation(value = "批量查找区域获取名字",notes = "批量查找区域获取名字")
    @ApiImplicitParam(name = "ids",value = "ids:以逗号隔开",required = true,paramType = "query", dataType = "String")
    public Result selectAreaNameBatch(@RequestParam("ids") String ids){
        if(StrUtils.isEmpty(ids)){
            return new Result(CodeEnum.FAIL_100002 );
        }
        String[] arr = ids.split(",");
        List<String> areaNameList = iSysAreaService.selectAreaNameBatchIds(Arrays.asList(arr));
        return new Result<>(areaNameList);

    }


    /**
     * 获取所有的省
     * @return
     */
    @ApiOperation(value = "获取一级信息",notes = "获取一级信息")
    @PostMapping(value = "/getAreaStair")
    public Result<SysAreaDTO> getAreaStair(Long id){
        SysAreaDTO stairArea = iSysAreaService.getAreaStair(id);
        return new Result<>(stairArea);
    }

    /**
     * 获取所有的省
     * @return
     */
    @ApiOperation(value = "获取所有的省",notes = "获取所有的省")
    @PostMapping(value = "/getAllProvince")
    public Result<List<SysAreaDTO>> getAllProvince(){
        List<SysAreaDTO> allProvince = iSysAreaService.getAllProvince();
        return new Result<>(allProvince);
    }

    /**
     * 根据最下ID查询区域信息
     * @param id
     * @return
     */
    @ApiOperation(value = "根据最下ID查询区域信息",notes = "根据最下ID查询区域信息")
    @PostMapping(value = "/getParentArea")
    public Result<String> getParentArea(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        String  areaStr = iSysAreaService.getParentArea(id,new StringBuffer()).toString();
        return new Result<>(areaStr);
    }

    /**
     * 根据Id查询出下级所有信息
     * @param id
     * @return
     */
    @ApiOperation(value = "查询下级所有信息",notes = "根据Id查询出下级所有信息")
    @PostMapping(value = "/getInfoByCityId")
    public Result<List<SysAreaDTO>> getInfoByCityId(@RequestParam("id") Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        List<SysAreaDTO> areaInfoList = iSysAreaService.getInfoByCityId(id);
        return new Result<>(areaInfoList);
    }

    @PostMapping("/get")
    @ApiOperation(value = "根据id查找",notes = "根据id查找")
    @ApiImplicitParam(name = "id",value = "id",required = true,paramType = "query", dataType = "Long")
    public Result<SysAreaDTO> get(Long id){
        if(id == null){
            return new Result<>(CodeEnum.FAIL_100002 );
        }
        SysAreaDTO sysDictDTO = iSysAreaService.selectById(id);
        return new Result<>(sysDictDTO);
    }

    @ApiOperation(value = "区域树列表",notes = "区域树列表")
    @PostMapping("/queryAreaTreeList")
    public Result<List<SysAreaVO>> queryAreaTreeList(){
        List<SysAreaVO> areaTree = iSysAreaService.getAreaTree();
        new Result<>(areaTree);
        return new Result<>(areaTree);
    }

    @ApiOperation(value = "区域树列表（到市级）",notes = "区域树列表（到市级）")
    @PostMapping("/queryAreaTreeInCityStopList")
    public Result<List<SysAreaVO>> queryAreaTreeInCityStopList(){
        List<SysAreaVO> areaTree = iSysAreaService.queryAreaTreeInCityStopList();
        new Result<>(areaTree);
        return new Result<>(areaTree);
    }
}

